{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "developed-situation",
   "metadata": {},
   "source": [
    "### 为什么需要用Numpy？\n",
    "通过代码验证"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "secret-painting",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "import time\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "framed-stockholm",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = []\n",
    "for i in range(10000000):\n",
    "    a.append(random.random())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "individual-sandwich",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.06459999084472656"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t1 = time.time()\n",
    "sum1 = sum(a)\n",
    "t2 = time.time()\n",
    "t2 - t1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "plain-jesus",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'a' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-3-3c85526bfb4a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mb\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'a' is not defined"
     ]
    }
   ],
   "source": [
    "b = np.array(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "processed-sight",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.34200024604797363"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t4 = time.time()\n",
    "sum3 = np.sum(a)\n",
    "t5 = time.time()\n",
    "t5 - t4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "documentary-claim",
   "metadata": {},
   "source": [
    "### numpy常用属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "bronze-austria",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([[1, 2, 3],\n",
    "              [4, 5, 6]]) \n",
    "#  从python的二维数组创建 一个ndarray对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "centered-stone",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "dirty-survey",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.dtype  # 数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "conventional-anaheim",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.size # 元素个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "crazy-winning",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 3)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape  # 形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "chronic-marriage",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "24"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.nbytes  # 总字节数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "floppy-academy",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.itemsize   # 每一个元素的长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "contemporary-nursery",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = np.array([1,2,3,4])\n",
    "c = np.array(\n",
    "                [\n",
    "                    [\n",
    "                        [1,2,3],\n",
    "                        [4,5,6]\n",
    "                    ],\n",
    "\n",
    "                   [\n",
    "                        [1,2,3],\n",
    "                        [4,5,6]\n",
    "                   ]\n",
    "\n",
    "                ]\n",
    "            )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "valuable-laundry",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "discrete-estonia",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 2, 3)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "radio-crash",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.ndim  # 查看ndarray对象的维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "recorded-holiday",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "hindu-mediterranean",
   "metadata": {},
   "outputs": [],
   "source": [
    "#### 查看一个函数的文档，可以通过shift+tab键查看"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "maritime-alarm",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = np.array([1.7, 2.8, 3.2], dtype=np.int32)    \n",
    "# 当我们制定了类型之后，如果原始数据的类型精度比制定的精度高的时候，会截取能存放的部分（不是四舍五入）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "fitting-stroke",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "elder-pierre",
   "metadata": {},
   "source": [
    "### 注意书籍和网上文章对numpy函数的解释"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "attractive-cable",
   "metadata": {},
   "source": [
    "* 如果是描述为  ndarray.func()  那么就是用具体的ndarry对象来调用这个函数\n",
    "例如： ndarray.shape  就是表示用具体的数据对象来调用shape\n",
    "       data.shape\\c.shape\\b.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "focal-wholesale",
   "metadata": {},
   "source": [
    "* 如果前面没任何前缀，或者是numpy.开头\n",
    "例如下面：<br>\n",
    "     empty()<br>\n",
    "    np.empty()<br>\n",
    "    numpy.empty()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "federal-kingdom",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4.05133830e-322, 0.00000000e+000, 0.00000000e+000],\n",
       "       [0.00000000e+000, 0.00000000e+000, 3.97228779e-321],\n",
       "       [1.05699242e-307, 1.95821438e-306, 5.68175493e-322]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.empty((3,3))\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "pressed-adult",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4.05133830e-322, 0.00000000e+000, 0.00000000e+000],\n",
       "       [0.00000000e+000, 0.00000000e+000, 3.97228779e-321],\n",
       "       [1.05699242e-307, 1.95821438e-306, 5.68175493e-322]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.empty((3,3))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "characteristic-singer",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 0, 0, 0],\n",
       "       [0, 0, 1, 0, 0],\n",
       "       [0, 0, 0, 1, 0],\n",
       "       [0, 0, 0, 0, 1],\n",
       "       [0, 0, 0, 0, 0]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.eye(5, 5, k=1, dtype=np.int)    # 对角线矩阵， \n",
    "# k 是对角线的偏移量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "respiratory-building",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0.],\n",
       "       [0., 1., 0.],\n",
       "       [0., 0., 1.]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.identity(3)   # 当行和列等宽的时候 这个对角线矩阵叫做单位矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "talented-feeling",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.eye(3, 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "after-stability",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones([3, 4])   # 等价于  np.ones((3,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "medical-residence",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.7, 2. , 3. ],\n",
       "       [4. , 5. , 6. ]])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1.7, 2, 3],\n",
    "              [4, 5, 6]])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "shared-configuration",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1.],\n",
       "       [1., 1., 1.]])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones_like(a)   # xxx_like函数都是生成 和指定数组 同类型同形状的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "tough-tobago",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 0],\n",
       "       [0, 0, 0, 0],\n",
       "       [0, 0, 0, 0]])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros((3,4), dtype=np.int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "purple-album",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 9, 9, 9],\n",
       "       [9, 9, 9, 9],\n",
       "       [9, 9, 9, 9]])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.full((3,4), 9)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "complex-interest",
   "metadata": {},
   "source": [
    "### 从现有的数据中创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "norwegian-flesh",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([1,2,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "devoted-writing",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 9, 9, 9],\n",
       "       [9, 9, 9, 9],\n",
       "       [9, 9, 9, 9]])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.full((3,4), 9)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "current-catalog",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 9, 9, 9],\n",
       "       [9, 9, 9, 9],\n",
       "       [9, 9, 9, 9]])"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1 = np.array(data)\n",
    "data1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "amazing-momentum",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 9, 9, 9],\n",
       "       [9, 9, 9, 9],\n",
       "       [9, 9, 9, 9]])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2 = np.asarray(data)\n",
    "data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "listed-monitoring",
   "metadata": {},
   "outputs": [],
   "source": [
    "data[1] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "chief-triangle",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 9, 9, 9],\n",
       "       [0, 0, 0, 0],\n",
       "       [9, 9, 9, 9]])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "spiritual-celebrity",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 9, 9, 9],\n",
       "       [9, 9, 9, 9],\n",
       "       [9, 9, 9, 9]])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1   # data1 没有跟着data变化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "comic-uruguay",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 9, 9, 9],\n",
       "       [0, 0, 0, 0],\n",
       "       [9, 9, 9, 9]])"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2   # data2 跟着data变化了"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "narrative-hypothesis",
   "metadata": {},
   "source": [
    "**array和 asarray的区别： array创建一个新的数组，而asarray只是创建了一个引用，因此，原数据变化的时候 asarray创建的数组会跟着变化**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fourth-maple",
   "metadata": {},
   "source": [
    "### 创建固定范围的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "innocent-budget",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr, step = np.linspace(0,10, 5, retstep=True)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "identified-penguin",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0. ,  2.5,  5. ,  7.5, 10. ])"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "verified-theory",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.5"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "step"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "attended-laundry",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "narrative-nevada",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 2, 4, 6, 8])"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(10, step=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "tight-party",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "split-physiology",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([   1.,   10.,  100., 1000.])"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.logspace(0, 3, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "thorough-think",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.0, 1.0, 2.0, 3.0)"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math\n",
    "math.log10(1), math.log10(10),math.log10(100),math.log10(1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "royal-subdivision",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3])"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(0,4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "weird-information",
   "metadata": {},
   "source": [
    "### 创建随机数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "armed-mapping",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.08996864, 0.55824569],\n",
       "       [0.82040983, 0.57938257],\n",
       "       [0.20871458, 0.94986847],\n",
       "       [0.84579573, 0.08700993]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.rand(4, 2)   # 生产0-1之间  制定维度的随机数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "weekly-missouri",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5.20105903, 6.78612603, 6.54016683, 5.62330661],\n",
       "       [5.07649867, 9.55128494, 8.5279315 , 6.79237279],\n",
       "       [7.76669752, 9.46466923, 9.12204851, 5.31027297]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.uniform(5,10, size=(3,4))   # 根据我们指定的最小值和最大值生成指定形状的随机数组（浮点型）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "absent-fiber",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 9, 8, 8, 5],\n",
       "       [6, 5, 7, 7, 5],\n",
       "       [8, 7, 7, 6, 5],\n",
       "       [8, 6, 8, 7, 6]])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(5, 10, size=(4,5))   # 左闭右开区间，不包括最大值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "first-biography",
   "metadata": {},
   "source": [
    "### 正太分布"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "printable-planning",
   "metadata": {},
   "source": [
    "μ均值，σ方差，数据的扩散大小、波动范围，σ扩散得越开，波动越大"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "developmental-richardson",
   "metadata": {},
   "source": [
    "方差不是上限和下限，只是数据波动大小的一种衡量方式"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "promotional-restaurant",
   "metadata": {},
   "source": [
    "### 模拟身高数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "coordinated-point",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([152.24269398, 181.18494332, 156.37422036, 161.32858622,\n",
       "       170.0939111 , 162.36678482, 142.69767292, 165.03841708,\n",
       "       160.62023627, 169.73337078, 156.83571028, 167.52217171,\n",
       "       161.55979104, 138.31782908, 170.40525219, 175.13031575,\n",
       "       155.75190223, 142.33889064, 166.03396108, 160.55412083,\n",
       "       154.97576007, 166.54421127, 173.91685547, 162.04388724,\n",
       "       179.92058828, 175.98541458, 176.2603282 , 163.21113553,\n",
       "       160.49882279, 168.07575015, 163.36728172, 150.09735676,\n",
       "       150.70070582, 179.13281487, 162.48955873, 170.10503717,\n",
       "       159.44010538, 162.48023859, 170.05472231, 168.56182296,\n",
       "       167.86510523, 174.46275483, 187.62070643, 147.73414814,\n",
       "       181.63042246, 164.90571331, 169.90627704, 164.91030063,\n",
       "       144.54438305, 168.55128942, 166.32770988, 158.42831724,\n",
       "       154.64041735, 160.11190757, 165.45606978, 163.4969024 ,\n",
       "       162.91195133, 170.11275934, 164.26461812, 172.6107596 ,\n",
       "       198.94911101, 183.65132658, 177.95988449, 153.86241383,\n",
       "       146.76594961, 153.06240467, 160.12840669, 161.30303888,\n",
       "       171.17701988, 144.93349736, 168.66573928, 175.27855294,\n",
       "       187.99124487, 153.74307254, 156.47081791, 161.52372637,\n",
       "       182.19154563, 166.59703543, 154.92963165, 166.52137587,\n",
       "       155.40382607, 166.94430424, 172.00780207, 155.81244206,\n",
       "       163.87163915, 166.3379766 , 201.91779683, 162.87858587,\n",
       "       178.84066323, 174.63897938, 159.92111333, 178.79299242,\n",
       "       149.03756933, 170.52374808, 148.98730805, 153.33371723,\n",
       "       159.71589096, 147.7945202 , 160.98627771, 154.24723525])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.normal(165, 10, size=100)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "allied-conflict",
   "metadata": {},
   "source": [
    "### 模拟成绩， 2033班53个同学 三门课的成绩"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "acute-attendance",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[60.99446866, 77.60488162, 78.14946117],\n",
       "       [74.31548958, 63.02894425, 82.44871085],\n",
       "       [78.37705911, 63.0992212 , 72.99708124],\n",
       "       [80.96445013, 73.95597461, 73.55448983],\n",
       "       [62.84244618, 74.19225211, 74.68259227],\n",
       "       [74.34728855, 69.29247949, 73.51591663],\n",
       "       [67.13768175, 67.36723186, 63.75538896],\n",
       "       [64.94794175, 82.00738481, 56.66193116],\n",
       "       [75.83358209, 90.64382789, 67.65955923],\n",
       "       [74.96901023, 77.31005851, 67.73313807],\n",
       "       [58.16322166, 73.54414158, 64.07872253],\n",
       "       [91.60314523, 70.77157746, 73.43975515],\n",
       "       [60.37756173, 84.39662777, 77.60260717],\n",
       "       [66.86481174, 66.17904375, 63.24751643],\n",
       "       [58.62896333, 74.57090663, 81.51833078],\n",
       "       [80.99507045, 65.31105357, 68.14677341],\n",
       "       [58.91527432, 76.27993055, 56.55492014],\n",
       "       [69.32233155, 75.58635739, 83.49363937],\n",
       "       [69.63800602, 70.88083847, 70.55911692],\n",
       "       [53.97873202, 67.47240638, 85.79860903],\n",
       "       [60.1724354 , 74.19899015, 63.67339775],\n",
       "       [87.00067403, 66.23530177, 61.56782737],\n",
       "       [62.0165145 , 59.41737611, 66.17709662],\n",
       "       [61.24984207, 52.65892925, 62.30630883],\n",
       "       [76.73205121, 69.86383874, 82.55398306],\n",
       "       [85.08812814, 74.23442694, 85.40220707],\n",
       "       [74.42061733, 57.47136201, 67.02252315],\n",
       "       [77.0199699 , 55.93344839, 66.63401188],\n",
       "       [82.6373728 , 71.454812  , 71.18477076],\n",
       "       [79.11617419, 65.15923791, 80.83492891],\n",
       "       [71.25826196, 66.99124162, 77.5117892 ],\n",
       "       [72.8523964 , 82.05322963, 59.25795379],\n",
       "       [70.46071819, 78.52943107, 70.85578108],\n",
       "       [78.4728642 , 64.31998958, 63.22112432],\n",
       "       [71.28783818, 71.06880392, 51.54498486],\n",
       "       [66.56045327, 89.49962399, 82.7889589 ],\n",
       "       [68.13411115, 50.06218765, 86.18491978],\n",
       "       [45.47650856, 79.20394983, 85.03990073],\n",
       "       [69.4134477 , 70.96142526, 82.43860071],\n",
       "       [75.74586833, 56.2667197 , 87.67856799],\n",
       "       [68.10780672, 88.26354357, 98.58578829],\n",
       "       [58.02375594, 82.35989922, 62.03614344],\n",
       "       [89.60046749, 63.77720277, 49.28117459],\n",
       "       [79.04304187, 67.33482668, 87.27247024],\n",
       "       [68.49230246, 83.16267803, 49.82837925],\n",
       "       [83.51098153, 77.76034798, 62.11896366],\n",
       "       [72.61062098, 70.1745282 , 75.16542581],\n",
       "       [74.52414533, 61.24207742, 73.03827284],\n",
       "       [61.4654343 , 76.05977058, 75.30631928],\n",
       "       [90.30528374, 56.54854509, 71.03259217],\n",
       "       [79.17939407, 75.11623138, 79.71645327],\n",
       "       [65.2597185 , 69.71878608, 77.80437693],\n",
       "       [81.27567605, 65.9605259 , 70.84029438]])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.normal(70, 10, size=(53, 3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "experimental-internship",
   "metadata": {},
   "source": [
    "### 数组的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "judicial-barcelona",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "acting-replacement",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.arange(100).reshape((10,10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "further-cambridge",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],\n",
       "       [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],\n",
       "       [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],\n",
       "       [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],\n",
       "       [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],\n",
       "       [50, 51, 52, 53, 54, 55, 56, 57, 58, 59],\n",
       "       [60, 61, 62, 63, 64, 65, 66, 67, 68, 69],\n",
       "       [70, 71, 72, 73, 74, 75, 76, 77, 78, 79],\n",
       "       [80, 81, 82, 83, 84, 85, 86, 87, 88, 89],\n",
       "       [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "confidential-novelty",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],\n",
       "       [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],\n",
       "       [50, 51, 52, 53, 54, 55, 56, 57, 58, 59],\n",
       "       [70, 71, 72, 73, 74, 75, 76, 77, 78, 79]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1[1:8:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "executive-ukraine",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[12, 15, 18],\n",
       "       [32, 35, 38],\n",
       "       [52, 55, 58],\n",
       "       [72, 75, 78]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1[1:8:2, 2:9:3]    #  带步长的切片， 起始：结束：步长"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "loose-stupid",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "documentary-antenna",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.array([[1,2],[3,4],[5,6]])\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "numerical-tours",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[0, 1]   # 先行后列， 取得是具体的元素   data[行，列]    data[第一维, 第二维, 第三维, ....]\n",
    "# 二维数组里面的每一个元素是一个一维数组，  所以当我们取二维数组里面的某个元素的时候，我们拿到的应该就是一个一维数组\n",
    "# 格式  ndarray[行开始:行结束, 列开始:列结束]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "perfect-genre",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[0][1]   # 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "middle-fabric",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 4])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "impossible-traffic",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[1:3]   #  起始:结束   不包含结束"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "occasional-possibility",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 5])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[1:3, 0]   # 行从第1行切到第3行， 取第0列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "continuous-fitness",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "growing-blues",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2]])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[0:1]   #  切片,不改变维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "municipal-receiver",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[0]   # 取元素， 子元素是什么维度，他拿到的就是什么维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "adopted-mystery",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 4]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[1:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "identical-disclosure",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 4])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "grand-saturn",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "encouraging-token",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[0, 0:2] # "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "played-necessity",
   "metadata": {},
   "source": [
    "### 修改形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "chicken-hurricane",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.array([[1,2],[3,4],[5,6]])\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "united-drunk",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 2)"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "weird-financing",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3],\n",
       "       [4],\n",
       "       [5],\n",
       "       [6]])"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.reshape((6, 1))   # 元素个数和 形状要匹配"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "incoming-equation",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2 = np.arange(12)\n",
    "data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "governing-nickel",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  1],\n",
       "        [ 2,  3]],\n",
       "\n",
       "       [[ 4,  5],\n",
       "        [ 6,  7]],\n",
       "\n",
       "       [[ 8,  9],\n",
       "        [10, 11]]])"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2.reshape((3, 2, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "acute-stadium",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11]])"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2.reshape((1, 12))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "rational-romantic",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "addressed-basket",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 4, 5, 6]])"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.reshape((1,6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "public-accreditation",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6])"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.reshape(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "moved-intranet",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.array([[1,2],[3,4],[5,6]])\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "suffering-introduction",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1 = data.reshape((2, 3))   # reshape之后，原数据没有变化，但会返回新的数组\n",
    "data1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "japanese-pocket",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "earned-viewer",
   "metadata": {},
   "outputs": [],
   "source": [
    "data2 = data.resize((2, 3))   # resize不返回数据，但修改了原数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "fluid-collection",
   "metadata": {},
   "outputs": [],
   "source": [
    "data2   # 没有输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "north-terry",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "intensive-retirement",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6])"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.flatten()   # 讲数据拉平"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "viral-metadata",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([159.1572918 , 176.42810528, 155.56559613, 167.46081046,\n",
       "       170.53945542, 163.52994433, 160.07151668, 157.74450798,\n",
       "       174.27662127, 152.77824318])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "high = np.random.normal(165, 10, size=10)\n",
    "high"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "commercial-clearance",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([159, 176, 155, 167, 170, 163, 160, 157, 174, 152])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "high.astype(np.int)    # numpy的特点，有返回数据的函数一般不修改原数据， 没有返回数据的函数则修改了原数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "outdoor-aurora",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([159.16, 176.43, 155.57, 167.46, 170.54, 163.53, 160.07, 157.74,\n",
       "       174.28, 152.78])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "high.round(2)    # ndarray.round()  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "fantastic-forge",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([159.16, 176.43, 155.57, 167.46, 170.54, 163.53, 160.07, 157.74,\n",
       "       174.28, 152.78])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.round(high, 2)   # numpy.round()   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "engaging-volume",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.arange(12).reshape((3,4))\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "jewish-corruption",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  4,  8],\n",
       "       [ 1,  5,  9],\n",
       "       [ 2,  6, 10],\n",
       "       [ 3,  7, 11]])"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.T  # 数组的转置,  以主轴翻转， 行变成列  列变成行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "moral-winner",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 4, 7])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np \n",
    "np.arange(1, 10, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "catholic-north",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2 = np.arange(12).reshape(3, 4)\n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "variable-wings",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 4)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "strong-synthesis",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "grave-mainland",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "marine-effectiveness",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  2,  4,  6,  8, 10])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(0, 12, 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "mechanical-television",
   "metadata": {},
   "source": [
    "### 逻辑运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "magnetic-survivor",
   "metadata": {},
   "outputs": [],
   "source": [
    "temp = np.random.randint(0,10, size=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "bronze-canberra",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 6, 4, 0, 5, 3, 4, 9, 5, 1])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "extra-benchmark",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True, False,  True,  True, False,  True,  True, False, False,\n",
       "        True])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp < 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "southwest-stereo",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 4, 0, 3, 4, 1])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp[temp < 5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "employed-there",
   "metadata": {},
   "outputs": [],
   "source": [
    "boolArr = np.array([ True, False,  True])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "thermal-bennett",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.all(boolArr, keepdims=True)    # 判断的是  数组里面所有内容是否均为True，  判断数据是否有空置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "level-pleasure",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0, 1, 3, 4, 5, 6, 9]), array([0, 9, 5, 2, 4, 1, 7], dtype=int64))"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unique(temp, return_index=True)   # 去掉重复元素并排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "incorporate-lotus",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 6, 4, 0, 5, 3, 4, 9, 5, 1])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "amber-giant",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 0, 0, 0, 0, 0, 1, 0, 0])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(temp > 5, 1, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "unauthorized-dinner",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "future-lecture",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 9, 4, 3],\n",
       "       [8, 2, 8, 1],\n",
       "       [2, 6, 3, 4]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0, 10, size=(3,4))\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "geographic-flood",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "pretty-template",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "adaptive-texture",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 9, 8, 4])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.max(axis=0)   # axis=0 按列比较"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "sitting-timber",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([9, 8, 6])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.max(axis=1)   # axis=0 按行运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "alive-aging",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 2, 3, 1])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.min(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "color-density",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 1, 2])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.min(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "desperate-recognition",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.833333333333333"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "meaning-kidney",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6.        , 5.66666667, 5.        , 2.66666667])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.mean(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "governmental-fraction",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.7028791233711424"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.std()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "incorporated-aspect",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 9, 4, 3],\n",
       "       [8, 2, 8, 1],\n",
       "       [2, 6, 3, 4]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "meaning-interim",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.argmax()     # 最大值的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "constitutional-cover",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.argmin()    # 最小值的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "mighty-bradley",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 1, 2], dtype=int64)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.argmax(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "strange-iraqi",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [5, 6, 1]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([[1,2,3], [5,6,1]])\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "ongoing-child",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 3, 4],\n",
       "       [6, 7, 2]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr + 1       # 广播"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "legal-niger",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [4, 5, 0]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "light-remark",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  4,  6],\n",
       "       [10, 12,  2]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr * 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "geological-lafayette",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.5, 1. , 1.5],\n",
       "       [2.5, 3. , 0.5]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr / 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "atmospheric-winter",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.arange(12,0, -1).reshape(3,4)\n",
    "arr2 = np.arange(12).reshape(3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "chief-canada",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[12, 11, 10,  9],\n",
       "        [ 8,  7,  6,  5],\n",
       "        [ 4,  3,  2,  1]]),\n",
       " (3, 4))"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1, arr1.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "prompt-estonia",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[ 0,  1,  2,  3],\n",
       "        [ 4,  5,  6,  7],\n",
       "        [ 8,  9, 10, 11]]),\n",
       " (3, 4))"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2, arr2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "established-infection",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[12, 12, 12, 12],\n",
       "       [12, 12, 12, 12],\n",
       "       [12, 12, 12, 12]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 + arr2    # 对应的位置相加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "rubber-french",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.arange(3,0, -1).reshape(3,1)\n",
    "arr2 = np.arange(12).reshape(3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "desperate-focus",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3],\n",
       "       [2],\n",
       "       [1]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "handy-quality",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "lucky-folder",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 3,  4,  5,  6],\n",
       "       [ 6,  7,  8,  9],\n",
       "       [ 9, 10, 11, 12]])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 + arr2   #  将arr1的列扩展到了arr2每一列进行运算， 将arr1扩展成3x4 和 arr2进行运算\n",
    "# 3x1 + 3x4   会将3x1 扩展成3x4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "expected-replication",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.arange(4,0, -1).reshape(1,4)\n",
    "arr2 = np.arange(12).reshape(3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "dying-mechanism",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 3, 2, 1]])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "higher-africa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "elect-aging",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4,  4,  4,  4],\n",
       "       [ 8,  8,  8,  8],\n",
       "       [12, 12, 12, 12]])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 + arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "identified-coral",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 广播： 边长为1的边  向对方看齐"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "classified-sydney",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.array([1])\n",
    "arr2 = np.arange(12).reshape(3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "final-wilson",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1])"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "colonial-aberdeen",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "strange-internet",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8],\n",
       "       [ 9, 10, 11, 12]])"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 + arr2   # 因为arr1的长宽都是1  所以他可以两边都扩展  所以最终是扩赞成 3x4的arr1 去和arr2做运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "parliamentary-yorkshire",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.array([[1,2]])\n",
    "arr2 = np.arange(12).reshape(3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "acquired-journal",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2]])"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "nasty-paris",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "touched-arlington",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "operands could not be broadcast together with shapes (1,2) (3,4) ",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-70-d972d21b639e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0marr1\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0marr2\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m: operands could not be broadcast together with shapes (1,2) (3,4) "
     ]
    }
   ],
   "source": [
    "arr1 + arr2   # 因为arr1 的列是2 因此无法拓展成4  所以无法预算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "chronic-shipping",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_score = np.random.normal(70,10, size=(10,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "editorial-cheese",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_score = stu_score.astype(np.int32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "incorporate-invite",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[89, 87],\n",
       "       [71, 77],\n",
       "       [61, 72],\n",
       "       [69, 61],\n",
       "       [63, 60],\n",
       "       [61, 77],\n",
       "       [66, 72],\n",
       "       [69, 64],\n",
       "       [82, 79],\n",
       "       [66, 77]])"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "macro-russia",
   "metadata": {},
   "outputs": [],
   "source": [
    "weight = np.array([0.7, 0.3])    # 平时成绩和期末成绩"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "intelligent-firmware",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([88.4, 72.8, 64.3, 66.6, 62.1, 65.8, 67.8, 67.5, 81.1, 69.3])"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.matmul(stu_score,  weight)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "liked-check",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "brazilian-cattle",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.array([[1,2],\n",
    "                  [3,4],\n",
    "                  [5,6]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "infinite-lewis",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr2 = np.array([[3,2,1],\n",
    "                 [1,2,3]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "sweet-jackson",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "adjustable-horizontal",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 2, 1],\n",
       "       [1, 2, 3]])"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cognitive-collins",
   "metadata": {},
   "source": [
    "![](images/Snipaste_2021-09-30_11-21-55.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "turned-infrared",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 5,  6,  7],\n",
       "       [13, 14, 15],\n",
       "       [21, 22, 23]])"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.matmul(arr1, arr2)   # 注意，矩阵乘法  MxN * N*L  = MxL    并且， 中间的那个边，必须相等"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "limiting-cruise",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr2 = np.array([[3,2,1],\n",
    "                 [1,2,3],\n",
    "                 [1,2,3]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "encouraging-context",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 3)"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "inside-commissioner",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 2)"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "aerial-grave",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[14, 20],\n",
       "       [22, 28],\n",
       "       [22, 28]])"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.matmul(arr2, arr1)    # 矩阵乘法  谁写前面和后面是有区别的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "local-corps",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "alleged-crossing",
   "metadata": {},
   "source": [
    "### 合并与分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "former-motor",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ones = np.ones((3,4))\n",
    "ones"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "interpreted-cleaning",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "zeros = np.zeros((3,4))\n",
    "zeros"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "empirical-hardwood",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1., 0., 0., 0., 0.],\n",
       "       [1., 1., 1., 1., 0., 0., 0., 0.],\n",
       "       [1., 1., 1., 1., 0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate((ones, zeros), axis=1)   # 将两个数组在行方向上进行拼接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "optical-wichita",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.],\n",
       "       [0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate((ones, zeros), axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "tired-tuning",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.],\n",
       "       [0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate((ones, zeros))   # 默认axis是0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "silver-convert",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1., 0., 0., 0., 0.],\n",
       "       [1., 1., 1., 1., 0., 0., 0., 0.],\n",
       "       [1., 1., 1., 1., 0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hstack((ones, zeros))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "occupational-logan",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.],\n",
       "       [0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vstack((ones, zeros))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "exposed-special",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],\n",
       "       [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],\n",
       "       [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],\n",
       "       [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],\n",
       "       [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],\n",
       "       [50, 51, 52, 53, 54, 55, 56, 57, 58, 59],\n",
       "       [60, 61, 62, 63, 64, 65, 66, 67, 68, 69],\n",
       "       [70, 71, 72, 73, 74, 75, 76, 77, 78, 79],\n",
       "       [80, 81, 82, 83, 84, 85, 86, 87, 88, 89],\n",
       "       [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.arange(100).reshape((10,10))\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "binary-hypothesis",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],\n",
       "        [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]]),\n",
       " array([[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],\n",
       "        [30, 31, 32, 33, 34, 35, 36, 37, 38, 39]]),\n",
       " array([[40, 41, 42, 43, 44, 45, 46, 47, 48, 49],\n",
       "        [50, 51, 52, 53, 54, 55, 56, 57, 58, 59]]),\n",
       " array([[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],\n",
       "        [70, 71, 72, 73, 74, 75, 76, 77, 78, 79]]),\n",
       " array([[80, 81, 82, 83, 84, 85, 86, 87, 88, 89],\n",
       "        [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])]"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.split(data, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "focal-wales",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 0,  1,  2,  3,  4],\n",
       "        [10, 11, 12, 13, 14],\n",
       "        [20, 21, 22, 23, 24],\n",
       "        [30, 31, 32, 33, 34],\n",
       "        [40, 41, 42, 43, 44],\n",
       "        [50, 51, 52, 53, 54],\n",
       "        [60, 61, 62, 63, 64],\n",
       "        [70, 71, 72, 73, 74],\n",
       "        [80, 81, 82, 83, 84],\n",
       "        [90, 91, 92, 93, 94]]),\n",
       " array([[ 5,  6,  7,  8,  9],\n",
       "        [15, 16, 17, 18, 19],\n",
       "        [25, 26, 27, 28, 29],\n",
       "        [35, 36, 37, 38, 39],\n",
       "        [45, 46, 47, 48, 49],\n",
       "        [55, 56, 57, 58, 59],\n",
       "        [65, 66, 67, 68, 69],\n",
       "        [75, 76, 77, 78, 79],\n",
       "        [85, 86, 87, 88, 89],\n",
       "        [95, 96, 97, 98, 99]])]"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.split(data, 2, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "weekly-passage",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
